package com.example.draw;

import com.example.draw.algorithm.LotteryAlgorithm;
import com.example.exception.BusinessException;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class LotteryAlgorithmFactory {

	private final Map<String, LotteryAlgorithm> algorithmMap;

	@Autowired
	public LotteryAlgorithmFactory(List<LotteryAlgorithm> algorithms) {
		algorithmMap = algorithms.stream().collect(
				Collectors.toMap(LotteryAlgorithm::getAlgorithmType,
				                 Function.identity()
				)
		);
	}

	public LotteryAlgorithm getAlgorithm(String algorithmType) {
		LotteryAlgorithm algorithm = algorithmMap.get(algorithmType);
		if (algorithm == null) {
			throw new BusinessException("不支持的抽奖算法类型: " + algorithmType);
		}
		return algorithm;
	}

}
